<template>
    <div class="invitation-list-page">
        <el-card shadow="never" class="mt-4">
            <el-button type="primary" @click="onAddClick">新增</el-button>
            <div class="mt-4" v-loading="loading">
                <el-table :data="data">
                    <el-table-column label="ID" prop="id" width="80"></el-table-column>
                    <el-table-column label="描述" prop="describe"/>
                    <el-table-column label="续费天数" prop="length"/>
                    <el-table-column label="价格" prop="price"/>
                    <el-table-column label="苹果内购ID" prop="apple_store_id"/>
                    <el-table-column label="创建者" prop="admin_account"/>
                    <el-table-column label="创建时间" align="center">
                        <template slot-scope="{row}">{{ timeFormat(row.created_at) }}</template>
                    </el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="{row}">
                            <el-button type="text" @click="onEditClick(row)">编辑</el-button>
                            <el-button
                                type="text"
                                @click="onDeleteClick(row)"
                                class="red-color"
                            >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div v-if="totalElements > 0" class="footer">
                <pagination
                    ref="pagination"
                    :currentPage="form.page"
                    :pageSize="form.size"
                    :handleCurrentChange="onCurrentChange"
                    :handlePageSizeChange="onPageSizeChange"
                    :totalElements="totalElements"
                ></pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import {
    Table,
    TableColumn,
} from 'element-ui';
import Pagination from '@/components/base/Pagination';
import TimeFormats from '@/helpers/TimeFormats';

export default {
    components: {
        ElTable: Table,
        ElTableColumn: TableColumn,
        Pagination,
    },
    data() {
        return {
            loading: false,
            data: [1],
            form: {
                page: 1,
                size: 10,
            },
            totalElements: 0,
        };
    },
    methods: {
        onSubmit() {
            this.form.page = 1;
            this.fetchData();
        },
        onResetFilters() {
            this.$refs.form.resetFields();
        },
        onCurrentChange(val) {
            this.form.page = val;
            this.fetchData();
        },
        onPageSizeChange(val) {
            this.form.size = val;
            this.form.page = 1;
            this.fetchData();
        },
        fetchData() {
            this.loading = true;
            API.get('product', { params: this.form }).then((res) => {
                this.data = res.content;
                this.totalElements = res.pagination.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        onAddClick() {
            this.$router.push({
                name: 'ExpireManage.Add',
            });
        },
        onEditClick({id}) {
            this.$router.push({
                name: 'ExpireManage.Add',
                params: { id: id },
            });
        },
        onDeleteClick(row) {
            $ele.$confirm('确定删除该有效期配置吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                API.delete(`product/${row.id}`, {
                }).then(() => {
                    $ele.$message.success('删除成功');
                    this.fetchData();
                }).finally(() => {
                    this.loading = false;
                });
            }).catch(() => { });
        },
        timeFormat(val) {
            return val ? new TimeFormats().convertTime('', val) : '';
        },
    },
    mounted() {
        this.fetchData();
    },
};
</script>

<style lang="scss" scoped>
.invitation-list-page {
    .filter {
        .input-width {
            max-width: 300px;
        }
    }
    .logo {
        width: 100px;
        height: 100px;
    }
    .footer {
        height: 44px;
        background: #f7f7f7;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
    }
    .image-slot {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        background: #f5f7fa;
        color: #909399;
    }
}
</style>
